package com.society.sgsc.dict.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.society.sgsc.dict.mapper.DictItemMapper;
import com.society.sgsc.dict.service.DictItemService;
import com.society.sgsc.entities.DictItem;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


@Service
@Transactional
public class DictItemServiceImpl extends ServiceImpl<DictItemMapper, DictItem> implements DictItemService {

    @Resource
    DictItemMapper dictItemMapper;

    /**
     * 添加字典详情信息
     * @param dictItem 添加的对象
     * @return Integer 是否有数据
     * @throws Exception
     */
    @Override
    public int saveDictItem(DictItem dictItem) throws Exception {
        //通过字典类型获取字典详情信息
        QueryWrapper<DictItem> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictItem.getDictType());
        wrapper.eq("dict_tag", dictItem.getDictTag());
        wrapper.eq("dict_val", dictItem.getDictVal());
        DictItem one = dictItemMapper.selectOne(wrapper);
        //判断是否有数据
        if (null == one) {//可以添加数据
            int insert = dictItemMapper.insert(dictItem);
            return insert;
        }
        return 0;

    }

    /**
     * 根据字典类型查询对应的的字典详情信息
     * @param dictType 字典类型
     * @return  List<DictItem> 返回对应的详细字典信息的集合
     * @throws Exception
     */
    @Override
    public List<DictItem> getDictItemsByDictType(String dictType) throws Exception {
        QueryWrapper<DictItem> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dictType);
        return dictItemMapper.selectList(wrapper);
    }
}
